/*
 * HashBucket.h
 *
 *  Created on: Oct 18, 2012
 *      Author: Federico Orquera
 *
 *  Maneja la estructura de bucket en memoria.
 *
 */

#ifndef HASHBUCKET_H_
#define HASHBUCKET_H_

//#define maxRecordsPerBucket 3

#include <list>
#include <string>
#include <memory>
#include "HashRecord.h"
#include "HashBucketSerializer.h"
//#include "HashFile.h"

class HashBucketSerializer;
class HashFile;

class HashBucket
{
public:
    typedef unsigned int TypeTD;        // renombrar
    typedef unsigned int BucketID;      // renombrar
    typedef std::list< std::shared_ptr <HashRecord> > HR_LIST;
    typedef std::shared_ptr< HR_LIST > HR_LIST_ptr;
    typedef std::shared_ptr< HashRecord > HR_ptr;
    friend class HashBucketSerializer;
    friend class HashFile;

private:
    static HR_LIST_ptr emptyRecordListPointer;
    static HashBucketSerializer m_serializer; // ( para calcular overflow )

    size_t      m_maxCapacity;
    BucketID    m_bucketId;
    TypeTD      m_td;
    HR_LIST_ptr m_records;

    // HashRecord* records[maxRecordsPerBucket];
    // RecordIndex numberOfRecords;
    // BucketID GenerateID();

public:
    HashBucket( size_t capacity, BucketID bucketId, TypeTD td,
            HR_LIST_ptr &records = emptyRecordListPointer );

    // constructor para el serializador, con parámetros reducidos
    HashBucket( TypeTD td, HR_LIST_ptr &records );
    ~HashBucket();

    BucketID getBucketNr() const;
    TypeTD getTd() const;
    void setTd( TypeTD td );
    bool addRegister( RecordID id, const std::string &frase,
            const std::string &apellidoAutor, const std::string &nombreAutor );
    HashRecord* getRecord( RecordID id );                // TODO: renombrar a search
    bool recordExists( RecordID id );
    void deleteRecord( RecordID id );

    HR_LIST getContent()
    {
        return *m_records;
    }

    void eraseContent()
    {
        m_records->clear();
    }
//    RecordIndex getCount();
//    bool isFull();

private:

    // set ID para el HashFile
    void setBucketNumber( BucketID id )
    {
        m_bucketId = id;
    };

    // set Size para el HashFile
    void setMaxCapacity( size_t newCapacity )
    {
        m_maxCapacity = newCapacity;
    };
};

#endif /* HASHBUCKET_H_ */
